﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NTTDATA.BEENChina.ResultCertification.Config;

namespace NTTDATA.BEENChina.ResultCertification.Checker
{
    public class VerfityFactory
    {
        //public static EntryResult Make(string categoryid,string result)
        //{
        //    //来自配置DB
            
        //    //根据 category id 拆分 录入结果
        //    //
        //    //list<string> result
        //    //可能是多个日期，多个地址等


        //    //来自配置DB
        //    //根据categoryid 生成 verfity 类
        //    List<string> entrys = new List<string>(){"example result"};//= Result.Split
        //    EntryResult entryResult = new ChildImage();
             
        //    //添加装饰类
        //    return Decorate(categoryid, entryResult);

        //}

        public static IEnumerable<Verfity> Create(string categoryId)
        {            
            //List<string> decoratorClassName =...
            //for ()
            //{
            //  string classname


            return null;

        }


        public static VerfityAttribute GetVerfityAttr( Type type)
        {
            System.Reflection.MemberInfo info = type;
            VerfityAttribute attribute = (VerfityAttribute)Attribute.GetCustomAttribute(info, typeof(VerfityAttribute));

            return attribute;
        }

        #region 获取检查类子类



        /// <summary>
        /// 获得全部的检查类
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Type> GetVerfitySubClass()
        {
            IEnumerable<Type> classTypes = Assembly.GetAssembly(typeof(Verfity)).GetTypes().Where(t => t.IsSubclassOf(typeof(Verfity)));

            return classTypes;
        }


        /// <summary>
        /// 获取相应种类的检查类
        /// 日期，地址。。。
        /// </summary>
        /// <param name="categoryType"></param>
        /// <returns></returns>
        public static IEnumerable<Type> GetVerfitySubClass(EnumCategoryType categoryType)
        {
           IEnumerable<Type> allVerfitys = GetVerfitySubClass();

           IEnumerable<Type> customerVerfitys= allVerfitys.Where(t =>
                                  {
                                 
                                      VerfityAttribute attribute =  GetVerfityAttr(t);

                                      if (attribute != null)
                                      {
                                          if (attribute.CategoryType == categoryType)
                                          {
                                              return true;
                                          }
                                      }

                                      return false;
                                  });

            return customerVerfitys;
        }

        /// <summary>
        /// 获取相应种类和模板标记的检查类
        /// </summary>
        /// <param name="categoryType"></param>
        /// <param name="model">是否模板</param>
        /// <returns></returns>
        public static IEnumerable<Type> GetVerfitySubClass(EnumCategoryType categoryType,bool model)
        {
            IEnumerable<Type> allVerfitys = GetVerfitySubClass();

            IEnumerable<Type> customerVerfitys = allVerfitys.Where(t =>
            {
                
                VerfityAttribute attribute = GetVerfityAttr(t);

                if (attribute != null)
                {
                    if (attribute.CategoryType == categoryType && attribute.Model == model)
                    {
                        return true;
                    }
                }

                return false;
            });

            return customerVerfitys;
        }

        #endregion


        public static Verfity CreateVerfityInstance(string verfityType)
        {
             return    (Verfity)Activator.CreateInstance(Type.GetType(verfityType));
        }

    }
}
